<template>
  <XtxDialog :title="`${ form.id ? '修改': '添加' }收货地址`" v-model="dialogVisible">
    <div class="address-edit">
      <div class="xtx-form">
        <div class="xtx-form-item">
          <div class="label">收货人：</div>
          <div class="field">
            <input v-model="form.receiver" class="input" placeholder="请输入收货人" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">手机号：</div>
          <div class="field">
            <input v-model="form.contact" class="input" placeholder="请输入手机号" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地区：</div>
          <div class="field">
            <XtxCity :fullLocation="form.fullLocation" @change="getAddressCode" placeholder="请选择所在地区" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">详细地址：</div>
          <div class="field">
            <input v-model="form.address"  class="input" placeholder="请输入详细地址" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">邮政编码：</div>
          <div class="field">
            <input v-model="form.postalCode" class="input" placeholder="请输入邮政编码" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地址标签：</div>
          <div class="field">
            <input v-model="form.addressTags"  class="input" placeholder="请输入地址标签，逗号分隔" />
          </div>
        </div>
      </div>
    </div>
    <template v-slot:footer>
      <XtxButton @click="dialogVisible = false" type="gray" style="margin-right:20px">取消</XtxButton>
      <XtxButton @click="submit" type="primary">确认</XtxButton>
    </template>
  </XtxDialog>
</template>

<script>
// 作业：自己添加表单校验
// 作业：默认收货地址(如果是第一个添加的收货地址设置为默认)
import { reactive, ref } from 'vue'
import { addAddress, editAddress } from '@/api/order'
import Message from '@/components/library/Message'
export default {
  name: 'AddressEdit',
  emits: ['change'],
  setup (props, { emit }) {
    const dialogVisible = ref(false)
    // 打开函数
    const open = (address) => {
      // 打开对话框
      dialogVisible.value = true
      // 两种情况： 1. 修改  2. 添加
      if (address) {
        // 情况1：修改 - 填充表单
        console.log('修改', address)
        for (const key in address) {
          form[key] = address[key]
        }
      } else {
        // 情况2：添加 - 清空表单
        // console.log('添加', address)
        for (const key in form) {
          form[key] = ''
        }
        form.isDefault = 0
      }
    }

    // 后端所需的表单数据
    const form = reactive({
      id: '',
      receiver: '', // 测试 收货人姓名
      contact: '', //  联系方式
      provinceCode: '', //  所在省份编码
      cityCode: '', //  所在城市编码
      countyCode: '', //  所在区/县编码
      address: '', //  详细地址
      postalCode: '', //  邮政编码
      addressTags: '', //  地址标签,以英文逗号分割
      isDefault: 0, //  是否为默认，0为是，1为否
      // ----------
      fullLocation: ''
    })

    // 城市选择组件
    const getAddressCode = (address) => {
      // 获取接口所需的省市区编码
      form.provinceCode = address.provinceCode
      form.countyCode = address.countyCode
      form.cityCode = address.cityCode
      // 用于用户展示的中文 省市区
      form.fullLocation = address.fullLocation
    }

    // 提交表单
    const submit = async () => {
      if (form.id) {
        // 情况1：修改 - 发送修改地址请求，需要 id
        await editAddress(form)
      } else {
        // 情况2：添加 - 发送添加地址请求，无需 id
        await addAddress(form)
      }
      // 公共部分：提示和隐藏对话框
      Message({ type: 'success', message: `${form.id ? '修改' : '添加'}收货地址成功` })
      dialogVisible.value = false
      // 通知父组件
      emit('change', form)
    }

    return { dialogVisible, open, form, getAddressCode, submit }
  }
}
</script>

<style scoped lang="less">
.address-edit {
  .xtx-dialog {
    :deep(.wrapper) {
      width: 780px;
      .body {
        font-size: 14px;
      }
    }
  }
  .xtx-form {
    padding: 0;
    input {
      outline: none;
      &::placeholder {
        color: #ccc;
      }
    }
  }
  .xtx-city {
    width: 320px;
    :deep(.select) {
      height: 50px;
      line-height: 48px;
      display: flex;
      padding: 0 10px;
      justify-content: space-between;
      .placeholder {
        color: #ccc;
      }
      i {
        color: #ccc;
        font-size: 18px;
      }
      .value {
        font-size: 14px;
      }
    }
    :deep(.option) {
      top: 49px;
    }
  }
}
</style>
